package com.wngbms.future;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;

@Slf4j
public class Demo1 {

    public static final int SLEEP_GAP = 5000;

    public static void main(String[] args) throws InterruptedException {

        MainJob mainJob = new MainJob();
        Thread mainThread = new Thread(mainJob);
        mainThread.setName("drink tea");
        mainThread.start();

        Callable<Boolean> hotJob = new HotWaterJob();
        Callable<Boolean> washJob = new WashJob();

        DefaultEventExecutorGroup group = new DefaultEventExecutorGroup(2);

        Future<Boolean> future = group.submit(hotJob);

        future.addListener(new GenericFutureListener<Future<? super Boolean>>() {
            @Override
            public void operationComplete(Future<? super Boolean> future) throws Exception {
                if (future.isSuccess()) {
                    mainJob.waterOk = true;
                    log.info("hot water is ok");
                    mainJob.drinkTea();
                } else {

                }
            }
        });

        Future<Boolean> future1 = group.submit(washJob);

        future1.addListener(new GenericFutureListener<Future<? super Boolean>>() {
            @Override
            public void operationComplete(Future<? super Boolean> future) throws Exception {
                if (future.isSuccess()) {
                    mainJob.cupOk = true;
                    log.info("wash done");
                    mainJob.drinkTea();
                } else {

                }
            }
        });
    }

    static class WashJob implements Callable<Boolean> {

        @Override
        public Boolean call() throws Exception {
            try {
                log.info("洗茶壶");
                log.info("洗茶杯");
                log.info("拿茶叶");
                //线程睡眠一段时间，代表清洗中
                Thread.sleep(SLEEP_GAP / 5);
                log.info("洗完了");

            } catch (InterruptedException e) {
                log.info(" 清洗工作 发生异常被中断.");
                return false;
            }
            log.info(" 清洗工作  运行结束.");
            return true;
        }
    }

    static class HotWaterJob implements Callable<Boolean> //①
    {

        @Override
        public Boolean call() throws Exception //②
        {
            try {
                log.info("水壶加水");
                log.info("灌上凉水");
                log.info("放在火上");

                //线程睡眠一段时间，代表烧水中
                Thread.sleep(SLEEP_GAP);
                log.info("水开了");
            } catch (InterruptedException e) {
                log.info(" 发生异常被中断.");
                return false;
            }
            log.info(" 烧水工作，运行结束.");
            return true;
        }
    }

    //泡茶线程
    static class MainJob implements Runnable {

        volatile boolean waterOk = false;
        volatile boolean cupOk = false;
        int gap = SLEEP_GAP / 10;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(gap);
                    log.info("读书中......");
                } catch (InterruptedException e) {
                    log.info("发生异常被中断.");
                }
            }
        }

        public void drinkTea() {
            if (waterOk && cupOk) {
                log.info("泡茶喝，茶喝完");
                this.waterOk = false;
                this.gap = SLEEP_GAP * 100;
            } else if (!waterOk) {
                log.info("烧水 没有完成，没有茶喝了");
            } else if (!cupOk) {
                log.info("洗杯子  没有完成，没有茶喝了");
            }
        }
    }


}
